/* ----------------------------------------------------------------------------------------------------------------
 * Mirror
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.entity 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.TriangleCulling;
	import pure.engine.renderer.Renderer;
	import pure.engine.core.ns_despair;
	
	use namespace ns_despair
	
	/**
	 * @internal
	 * 
	 * 残像
	 * 
	 * @author Pure
	 */
final public class Mirror extends Sprite 
{
	public function Mirror()
	{
		this.mouseChildren = this.mouseEnabled = false;
	}

	
	
	// 深度
	public function get layer():int { return entity.m_layer; }
	public function get id():int { return entity.m_id; }
	
	
	// 滑鼠检测
	override public function get name():String { return entity.m_name; }
	override public function get x():Number { return super.x + entity.m_range; }
	override public function get y():Number { return super.y + entity.m_range; }
	
	

	// 绑定
	
	// [Hint] - 残像初次映射，无视渲染器状态
	ns_despair function binding(E:Entity):void
	{
		var R:Renderer;
		var F:Fragment;
		var i:int, len:int;
		var range:Number;
		
		entity       =  E;
		RList        =  E.m_rendererList;
		FDirtyList   =  E.m_fragmentDirtyList;
		numRenderer  =  E.m_numRenderer;
		range        =  E.m_range;

		for (i = 0; i < numRenderer; i++)
		{
			R             =  RList[i];
			F             =  Fragment.makeFragment();
			F.entity      =  E;
			F.renderer    =  R;
			FList[i]      =  F;
			this.addChild(F);
			
			if (R.m_visible)
			{
				F.bitmapData  =  R.m_imageB;
				F.x = F.y = - R.m_range;
			}
		}
	}
	
	
	// 更新
	
	// [Feature] - [-1]不可见，[0]画像不变，[1]画像变化，[2]范围变化
	// [Hint] - 残像停留在监视器中，对其更新
	//        - [0]        >>>> skip...
	//        - [-1][1][2] >>>> [0]
	ns_despair function update():void
	{
		var i:int, flag:int, tail:int;
		var F:Fragment;
		
		if ((tail = FDirtyList[numRenderer]) > 0)
		{
			for (i = 0; i < tail; i++)
			{
				flag = FDirtyList[i];
				
				// 未变化
				if(flag != 0)
				{
					// 画像
					if (flag == 1)
					{
						FList[i].bitmapData = RList[i].m_imageB;
					}
					// 范围
					else if (flag == 2)
					{
						F = FList[i];
						F.bitmapData = RList[i].m_imageB;
						// 碎片偏移量为渲染器的range
						F.x = F.y = - F.renderer.m_oldRange;
					}
					// 不可见
					else if (flag == -1)
					{
						FList[i].bitmapData = null;
					}
					FDirtyList[i] = 0;
				}
			}
		}
	}
	
	
	// 回收
	ns_despair function recycle():void
	{
		var F:Fragment;
		
		entity        =  null;
		RList         =  null;
		FDirtyList    =  null;
		FList.length  =  0;
		
		while (this.numChildren)
		{
			F = this.removeChildAt(0) as Fragment;
			F.recycle();
		}
		
		cachedMirrors[numMirror++] = this;
	}

	
	
	
	//======================
	// Mirror
	//======================

	
	// 生成残像
	ns_despair static function getMirror():Mirror
	{
		if (numMirror > 0)
		{
			numMirror--
			return cachedMirrors.pop();
		}
		return new Mirror();
	}


	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair static var cachedMirrors:Vector.<Mirror> = new Vector.<Mirror>();
	
	ns_despair static var numMirror:int;
	
	
	ns_despair var entity:Entity;
	
	ns_despair var RList:Vector.<Renderer>;
	
	ns_despair var FDirtyList:Vector.<uint>;
	
	ns_despair var FList:Vector.<Fragment> = new Vector.<Fragment>();
	
	ns_despair var numRenderer:int;
	
	
}
}